package com.example.bbgds.test;

import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by bbgds on 2016/3/7.
 */
public class LayoutBottom extends RelativeLayout{
    private RecyclerView mRecyclerView;
    private PersonAdapter mPersonAdapter;
    private VoiceAnimationItem mVoiceAnimationItem;

    private List<DataItem> mList = new ArrayList<DataItem>();

    public LayoutBottom(Context context) {
        this(context, null);
    }

    public LayoutBottom(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public LayoutBottom(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        View.inflate(context, R.layout.layout3, this);
        mRecyclerView = (RecyclerView) findViewById(R.id.listview);
        mRecyclerView.setHasFixedSize(true);
        mRecyclerView.setLayoutManager(new MyLinearLayoutManager(context));
        mPersonAdapter = new PersonAdapter();
        mRecyclerView.setAdapter(mPersonAdapter);
        DataItem item = new DataItem();
        item.mType = DataItem.TYPE_ANIMATION;
        mList.add(item);

        mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            //用来标记是否正在向最后一个滑动，既是否向右滑动或向下滑动
            boolean isSlidingToLast = false;

            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {

                Log.e("zhu", "onScrollStateChanged="  + newState);
                LinearLayoutManager manager = (LinearLayoutManager) recyclerView.getLayoutManager();
                // 当不滚动时
                if (newState == RecyclerView.SCROLL_STATE_IDLE) {
                    //获取最后一个完全显示的ItemPosition
                    int lastVisibleItem = manager.findLastCompletelyVisibleItemPosition();
                    int totalItemCount = manager.getItemCount();
                    Log.e("zhu","lastVisibleItem=" + lastVisibleItem +"," + totalItemCount +"," + isSlidingToLast);

                    // 判断是否滚动到底部，并且是向右滚动
                    if (lastVisibleItem == (totalItemCount - 1)) {
                        //加载更多功能的代码
                        Log.e("zhu", "howes right=" + manager.findLastCompletelyVisibleItemPosition());
                        Toast.makeText(getContext(), "加载更多", 0).show();
                    }
                }
            }
        });
    }


    public RecyclerView getRecyclerView(){
        return mRecyclerView;
    }

    public void addItem(View itemView, DataItem item, int itemHeight){
        if(!isBottom()){
            scrollToBottom();
        }
        mVoiceAnimationItem.addItem(itemView, item, itemHeight);
    }

    public void addItemToList(DataItem item){
        mList.add(1, item);
        mRecyclerView.setTranslationY(0);
        mPersonAdapter.notifyDataSetChanged();
    }

    public class PersonAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            if(viewType == DataItem.TYPE_TAG){
                RelativeLayout layout = new RelativeLayout(parent.getContext());
                RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
                View textView = View.inflate(parent.getContext(), R.layout.layout_tag, null);
                textView.setLayoutParams(lp);
                layout.addView(textView);
                layout.setAlpha(0.5f);
                layout.setLayoutParams(new RecyclerView.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
                return new TagViewHolder(layout, textView);
            }else if(viewType == DataItem.TYPE_TEXT){
                RelativeLayout layout = new RelativeLayout(parent.getContext());
                RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
                TextView textView = VoiceActivity.getTextView(parent.getContext(), null);
                textView.setLayoutParams(lp);
                layout.addView(textView);
                layout.setAlpha(0.5f);
                layout.setLayoutParams(new RecyclerView.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
                return new PersonViewHolder(layout, textView);
            }else if(viewType == DataItem.TYPE_ANIMATION){
                mVoiceAnimationItem = new VoiceAnimationItem(parent.getContext());
                mVoiceAnimationItem.setLayoutBottom(LayoutBottom.this);
                mVoiceAnimationItem.setLayoutParams(new RecyclerView.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
                return new AnimationViewHolder(mVoiceAnimationItem);
            }
            return null;
        }

        @Override
        public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
            if(holder instanceof PersonViewHolder){
                ((PersonViewHolder)holder).initData(position);
            }else if(holder instanceof TagViewHolder){
                ((TagViewHolder)holder).initData(position);
            }
        }

        @Override
        public int getItemCount() {
            return mList.size();
        }

        @Override
        public int getItemViewType(int position) {
            return mList.get(position).mType;
        }
    }


    private class PersonViewHolder extends RecyclerView.ViewHolder{
        private TextView textView;
        public PersonViewHolder(View itemView, TextView view) {
            super(itemView);
            textView = view;
        }

        public void initData(int position){
            DataItem item = mList.get(position);
            if(item.isVisible){
                textView.setVisibility(View.VISIBLE);
            }else{
                textView.setVisibility(View.INVISIBLE);
            }
            textView.setText(item.mText);
            RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams)textView.getLayoutParams();
            lp.leftMargin = 0;
            lp.rightMargin = 0;
            lp.removeRule(RelativeLayout.ALIGN_PARENT_RIGHT);
            lp.removeRule(RelativeLayout.ALIGN_PARENT_LEFT);
            if(item.isRight){
                lp.leftMargin = 200;
                lp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE);
            }else{
                lp.rightMargin = 200;
                lp.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
            }
            textView.setLayoutParams(lp);
        }
    }


    private class TagViewHolder extends RecyclerView.ViewHolder{
        private View textView;
        private View itemView;
        public TagViewHolder(View itemView, View tagView) {
            super(itemView);
            this.itemView = itemView;
            textView = tagView;
        }

        public void initData(int position){
            DataItem item = mList.get(position);
            RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams)textView.getLayoutParams();
            lp.leftMargin = 0;
            lp.rightMargin = 0;
            lp.removeRule(RelativeLayout.ALIGN_PARENT_RIGHT);
            lp.removeRule(RelativeLayout.ALIGN_PARENT_LEFT);
            if(item.isRight){
                lp.leftMargin = 200;
                lp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE);
            }else{
                lp.rightMargin = 200;
                lp.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
            }
            textView.setLayoutParams(lp);
        }
    }

    private class AnimationViewHolder extends RecyclerView.ViewHolder{
        public AnimationViewHolder(View itemView) {
            super(itemView);
        }
    }

    public static class MyLinearLayoutManager extends LinearLayoutManager {

        public MyLinearLayoutManager(Context context) {
            super(context);
            this.setReverseLayout(true);
        }

    }

    public boolean isBottom() {
        LinearLayoutManager lm = (LinearLayoutManager) mRecyclerView.getLayoutManager();
        if (lm.findViewByPosition(lm.findFirstVisibleItemPosition()).getTop() == 0 && lm.findFirstVisibleItemPosition() == 0) {
            return true;
        }
        return false;
    }

    private void scrollToBottom(){
        mRecyclerView.getLayoutManager().scrollToPosition(0);
    }
}
